package stvd.sign;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import org.springframework.security.crypto.codec.Base64;

/**
 * 通过RSA算法，以seedKey为随机数，生成priKey和pubKey
 *
 */
public class KeyGenerater {
	private byte[] priKey;
	private byte[] pubKey;
	private byte[] seedKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAi/a1sxaSYqoS5bw7uf2yNYkbQP429gZsu/QeALyCg9fMXQwlYq3T78WWMAWGoabToDJDtyoJzbRFmcggGIMvtQIDAQABAkA3/6xxC83j1eIbsrU+1AcyLjoORefdhXKJvMEoUEPz1ZK+N1GrjLdmXdESB5TwgFevw/2CG1d/5dds4UohmM6hAiEAyL6NvsxFbJm5HTXP/Bo7p/MVsGOcjd5twjWhgVZg8B0CIQCyfT/jZrEDSEpRZiT0LObMvPXcoxW7oxISdqAkZc3aeQIgUxhNpET6F9IqBiVYSoqxVvwQZktVaoQ/+rwodllDFyECIBY+I4MMHg0BqjlAmGRRYCWtepoXPLMYXD0uyUAf66pJAiEAqvXhZHWWhZHubxL/UusltcCxWXoAQfhpkHGU6eSoxNs=".getBytes();
	
	public void setSeed(String seed){
		seedKey = seed.getBytes() ;
	}
	public void generater() {
		try {
			KeyPairGenerator keygen = KeyPairGenerator .getInstance("RSA");
			SecureRandom secrand = new SecureRandom();
			secrand.setSeed(seedKey); // 初始化随机产生器
			keygen.initialize(1024, secrand);
			KeyPair keys = keygen.genKeyPair();
			PublicKey pubkey = keys.getPublic();
			PrivateKey prikey = keys.getPrivate() ;
						
			pubKey = Base64.encode(pubkey.getEncoded());
			priKey = Base64.encode(prikey.getEncoded());

			System.out.println("pubKey = " + new String(pubKey));
//			System.out.println(pubKey.length); 
			System.out.println("priKey = " + new String(priKey));
//			System.out.println(priKey.length);
		} catch (java.lang.Exception e) { 
			e.printStackTrace(); 
		} 
	}
	public byte[] getPriKey() { 
		return priKey;
	}
	
	public byte[] getPubKey() {
		return pubKey;
	}
	
	public static void main(String[] args){
		KeyGenerater keyGenerater = new KeyGenerater();
		keyGenerater.generater();
	}
}